/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package util;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;


public class ScheduleUtil {
    
    //TEST
    public static void main(String[] args) {
        System.out.println("MAIN");
        test1();            
    }
    
    //http://www.programcreek.com/2009/08/schedule-a-task-to-a-certain-time-and-make-java-app-run-as-a-windows-service-using-java-service-wrapper/
    public static void test1() {
        Timer timer = new Timer();
        TimerTask tt = new TimerTask(){
            public void run(){
                System.out.println("INICIANDO TAREA");
                Calendar cal = Calendar.getInstance(); //this is the method you should use, not the Date(), because it is desperated.
                int hour = cal.get(Calendar.HOUR_OF_DAY);//obtiene el numero de hora del dia, de 0 a 23
                int h = 16;
                if(hour == h){
                    System.out.println("Ejecutando la tarea si la hora del dia es "+h);
                }
                
                int min = cal.get(Calendar.MINUTE);
                int m = 5;
                if(min == m)
                    System.out.println("Ejecutando la tarea si los minutos del dia es "+m);
            }
        };
        timer.schedule(tt, 1000, 1000*2);// retarda la tarea 1 segundo, y luego ejecuta la tarea cada 2 segundos
    }
    
    //http://oreilly.com/java/archive/quartz.html
    public static void test2() {
        Timer timer = new Timer();
        TimerTask tt = new TimerTask(){
            public void run(){
                System.out.println("TAREA");                
            }
        };
        
        Calendar date = Calendar.getInstance();
        date.set(
          Calendar.DAY_OF_WEEK,
          Calendar.SUNDAY
        );
        date.set(Calendar.HOUR, 0);
        date.set(Calendar.MINUTE, 0);
        date.set(Calendar.SECOND, 0);
        date.set(Calendar.MILLISECOND, 0);
        
        // Programa la tarea para ejecutarse cada Domingo a la medianoche
        // Schedule to run every Sunday in midnight
        timer.schedule(
          tt,
          date.getTime(), // domingo a la medianoche
          1000 * 60 * 60 * 24 * 7 // 7 dias
        );
    }
}
